var app = getApp()
//常量数据配置
var ConstantData = require('../../../common/constant_data')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    numPerPage: 10,
    leseList: [],
    showList: false,
    selectIndex: 0,
    noLoginFlg:false,
    costRecord:[],
    userUseRecord:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let selectOrder=options.selectOrder?JSON.parse(decodeURIComponent(options.selectOrder)):'' 
    console.log('传递的',selectOrder,options)
    this.setData({
      selectOrder:options.selectOrder?selectOrder:options.selectOrder,
      selectIndex:Number(options.selectIndex) 
    })
    if(options.selectOrder){
    this.getApageLeaseRecord()
    this.getOrderDetail(selectOrder.orderCode)   
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  //获取订单
  getApageLeaseRecord: function (lode) {
    if (!app.globalData.networkIsCon) {
      this.setData({
        networkType: 'none'
      })
      return
    }
    if(app.globalData.loginData && app.globalData.loginData.nophone == 1) {
      //需要重新登录      
      this.setData({
        noLoginFlg: true
      })
      return
    }
    if (!lode) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      });
    }
    var that = this
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease/wxmini/record_list',
        method: 'post',
        header: header,
        data: {
          numPerPage: that.data.numPerPage,
          pageNum: that.data.pageNum
        },
        success: function (res) {
          console.log("userRecordsList-->", res);
          if (res.data.result == 0) {
            var oldList = that.data.leseList || []
            var newList = oldList.concat(res.data.data.data)
            newList = newList.filter(item => {
              return item.orderType != 1000
            })
            if (res.data.data.pageNum * that.data.numPerPage >= res.data.data.total) {
              that.setData({
                loading: false,
                loadingComplete: true,
                leseList: newList,
              });
            } else {
              that.setData({
                loading: true,
                loadingComplete: false,
                leseList: newList,
              });
            }
          } else {
            wx.showToast({
              title: '获取失败'
            })
          }
        },
        complete() {
          getApp().hideLoading()
        }
      })

    })

  },
    //点击切换订单
    onChangeOrder: function () {
      this.setData({
        showList: true
      })
    },
    onCloseList: function () {
      this.setData({
        showList: false
      })
    },
      // 点击选择订单
  onOrder: function (e) {
    let that = this
    this.setData({
      selectIndex: e.currentTarget.dataset.index,
      selectOrder: that.data.leseList[e.currentTarget.dataset.index],
      showList: false
    })
    this.getOrderDetail(that.data.selectOrder.orderCode)
  },
    //滚动加载下一页
    onBottom: function(){
      var that = this;
      if (that.data.loading && !that.data.loadingComplete) {
        that.setData({
          pageNum: that.data.pageNum + 1, //每次触发上拉事件，把searchPageNum+1  
        });
        that.getApageLeaseRecord();
      }
    },
  //获取详情信息
  getOrderDetail:function(orderCode) {
    let that =this
    if (!app.globalData.networkIsCon) {
      this.setData({
        networkType: 'none'
      })
      return
    }
    if(app.globalData.loginData && app.globalData.loginData.nophone == 1) {
      //需要重新登录      
      this.setData({
        noLoginFlg: true
      })
      return
    }
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + `/lease/recordDetail/${orderCode}`,
        method: 'get',
        header: header,
        success: function (res) {
          console.log("userRecordsList-->", res);
          if (res.data.result == 0) {
         that.setData({
          page_show:true,
          costRecord:res.data.data.costRecord,
          userUseRecord:res.data.data.userUseRecord
         })
          } else {
            wx.showToast({
              title: '获取失败'
            })
          }
        },
        complete() {
          getApp().hideLoading()
        }
      })
    })        
  },
  //查看收费规则
  toFeeRule(){
    wx.navigateTo({
      url: '/pkgNew/pages/feeRule/index?selectOrder='+`${this.data.selectOrder?encodeURIComponent(JSON.stringify(this.data.selectOrder)):''}`+`&noLoginFlg=${this.data.noLoginFlg}`,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})